#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPainter>
#include <QDebug>
#include <QFile>
#include <QMouseEvent>

#define BG_IMG ":/p18-key-led-test.png"
#define BASE_WIDTH_PIX 1800
#define BASE_HEIGHT_PIX (BASE_WIDTH_PIX / 2)
#define BASE_WIDTH_CM 60
#define BASE_HEIGHT_CM
#define CONV(CM) (CM * BASE_WIDTH_PIX / BASE_WIDTH_CM)


QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    enum ShapeType {
        Rectangle,
        Circle
    };

    struct ShapeInfo {
        ShapeType type;
        int x;
        int y;
        int width;
        int height;
        QColor color;
    };
    // 设置绘制矩形的参数
    void setRectangleParams(int x, int y, int width, int height, const QColor& color) {
        ShapeInfo info;
        info.type = Rectangle;
        info.x = x;
        info.y = y;
        info.width = width;
        info.height = height;
        info.color = color;
        shapes.append(info);
        update(); // 触发 paintEvent 重绘界面
    }

    // 设置绘制圆形的参数
    void setCircleParams(int x, int y, int radius, const QColor& color) {
        ShapeInfo info;
        info.type = Circle;
        info.x = x;
        info.y = y;
        info.width = 2 * radius;
        info.height = 2 * radius;
        info.color = color;
        shapes.append(info);
        update(); // 触发 paintEvent 重绘界面
    }

private:
    Ui::Widget *ui;

    QVector<ShapeInfo> shapes;

protected:
    void paintEvent(QPaintEvent *event) override {
        QWidget::paintEvent(event);
        QPainter painter(this);
        // 加载背景图片
        QImage backgroundImage(BG_IMG);
        if (!backgroundImage.isNull()) {
            // 绘制背景图片，缩放以适应窗口大小
            painter.drawImage(rect(), backgroundImage);
        }

        //for (const auto& shape : shapes) {
            ShapeInfo& shape = shapes.last();
            painter.setBrush(shape.color);
            painter.setPen(Qt::NoPen);

            switch (shape.type) {
            case Rectangle:
                painter.drawRect(shape.x, shape.y, shape.width, shape.height);
                break;
            case Circle:
                painter.drawEllipse(shape.x, shape.y, shape.width, shape.height);
                break;
            default:
                break;
            }
        //}
    }
    void mousePressEvent(QMouseEvent *event) override {
        int targetX = CONV(48);
        int targetY = CONV(5);
        int targetWidth = CONV(1);
        int targetHeight = CONV(2);

        // 检查鼠标点击位置是否在指定区域内
        if (event->x() >= targetX && event->x() <= targetX + targetWidth &&
            event->y() >= targetY && event->y() <= targetY + targetHeight) {
            // 读取 gpio24/value 文件
            QFile readFile("/sys/class/gpio/gpio24/value");
            if (readFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
                QByteArray data = readFile.readAll();
                readFile.close();
                int value = data.trimmed().toInt();

                // 写入 gpio24/value 文件
                QFile writeFile("/sys/class/gpio/gpio24/value");
                if (writeFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
                    qint64 writeResult;
                    if (value == 1) {
                        writeResult = writeFile.write("0");
                    } else {
                        writeResult = writeFile.write("1");
                    }
                    writeFile.close();

                    // 检查写入是否成功
                    if (writeResult > 0) {
                        // 再次读取 gpio24/value 文件
                        if (readFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
                            data = readFile.readAll();
                            readFile.close();
                            value = data.trimmed().toInt();

                            if (value == 1) {
                                setRectangleParams(targetX, targetY, targetWidth, targetHeight, QColor(Qt::green));
                            } else {
                                setRectangleParams(targetX, targetY, targetWidth, targetHeight, QColor(Qt::transparent));
                            }
                        }
                    }
                }
            }
        }
        // 处理 gpio125 区域点击事件
       int targetX125 = CONV(49);
       int targetY125 = CONV(5);
       int targetWidth125 = CONV(1);
       int targetHeight125 = CONV(2);

       // 检查鼠标点击位置是否在 gpio125 指定区域内
       if (event->x() >= targetX125 && event->x() <= targetX125 + targetWidth125 &&
           event->y() >= targetY125 && event->y() <= targetY125 + targetHeight125) {
           // 读取 gpio125/value 文件
           QFile readFile("/sys/class/gpio/gpio125/value");
           if (readFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
               QByteArray data = readFile.readAll();
               readFile.close();
               int value = data.trimmed().toInt();

               // 写入 gpio125/value 文件
               QFile writeFile("/sys/class/gpio/gpio125/value");
               if (writeFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
                   qint64 writeResult;
                   if (value == 1) {
                       writeResult = writeFile.write("0");
                   } else {
                       writeResult = writeFile.write("1");
                   }
                   writeFile.close();

                   // 检查写入是否成功
                   if (writeResult > 0) {
                       // 再次读取 gpio125/value 文件
                       if (readFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
                           data = readFile.readAll();
                           readFile.close();
                           value = data.trimmed().toInt();

                           if (value == 1) {
                               setRectangleParams(targetX125, targetY125, targetWidth125, targetHeight125, QColor(Qt::red));
                           } else {
                               setRectangleParams(targetX125, targetY125, targetWidth125, targetHeight125, QColor(Qt::transparent));
                           }
                       }
                   }
               }
           }
       }
        QWidget::mousePressEvent(event);
    }


};
#endif // WIDGET_H
